package com.good.jzhouse.controller;

import com.good.jzhouse.bean.*;
import com.good.jzhouse.dao.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/house")
public class VillageController {
    @Resource
    VillageDao vd;
    @Resource
    HouseTypeDao htd;
    @Resource
    AreaDao ad;
    @Resource
    RenovationDao rd;
    @Resource
    FacilitiesDao fd;
    @Resource
    LabelDao ld;
    @Resource
    LablelHouseDao lhd;
    @Resource
    HouseDao houseDao;
    @Resource
    SellhouseDao shd;
    @Resource
    BuyHouseDao bhd;
    @Resource
    MapJzDao mapJzDao;

    @RequestMapping("/listvillage")
    @ResponseBody
    public List<Village> houselist(Model m){
        return vd.findAll();
    }

    @RequestMapping("/listhousetype")
    @ResponseBody
    public List<HouseType> housetypelist(Model m){
        return htd.findAll();
    }

    @RequestMapping("/listarea")
    @ResponseBody
    public List<Area> arealist(Model m){
        return ad.findAll();
    }

    @RequestMapping("/listrenovation")
    @ResponseBody
    public List<Renovation> renovationlist(Model m){
        return rd.findAll();
    }

    @RequestMapping("/listfacilities")
    @ResponseBody
    public List<Facilities> facilitieslist(Model m){
        return fd.findAll();
    }


    @RequestMapping("/listlabel")
    @ResponseBody
    public List<Label> lablelist(Model m){
        return ld.selectType1();
    }

    @RequestMapping("/listlabel2")
    @ResponseBody
    public List<Label> lablelist2(Model m){
        return ld.selectType2();
    }

    @RequestMapping("/save")
    @ResponseBody
    public Boolean save(House house, int[] names, Integer etime, HttpSession s){
            house.setIndoorImg((String) s.getAttribute("imgPaths"));
            Date d = new Date();
            Timestamp t = new Timestamp(d.getTime());
            house.setBeginTime(t);
            if(houseDao.insert(house)>0){
                for (int i=0;i < names.length;i++){
                    LabelHouse lh = new LabelHouse(null,houseDao.findnewid(),new Label(names[i]));
                    lhd.insert(lh);
                }
                House h = houseDao.findnewid();
                Sellhouse sh = new Sellhouse(null,h,2,null,true,new Date(),etime);
                sh.setOnline(true);
                shd.insert(sh);
                MapJz m = new MapJz(null,h,112.248+(h.getId()*0.2),30.34+(h.getId()*0.2));
                mapJzDao.insert(m);
                s.setAttribute("imgPaths",null);
                return true;
            }else{
                return false;
            }

        }

    @RequestMapping("/save2")
    @ResponseBody
    public Boolean save2(House house,int[] names,Integer etime, HttpSession s){
        house.setIndoorImg((String) s.getAttribute("imgPaths"));
        Date d = new Date();
        Timestamp t = new Timestamp(d.getTime());
        house.setBeginTime(t);
        if(houseDao.insert(house)>0){
            for (int i=0;i < names.length;i++){
                LabelHouse lh = new LabelHouse(null,houseDao.findnewid(),new Label(names[i]));
                lhd.insert(lh);
            }
            House h = houseDao.findnewid();
            Sellhouse sh = new Sellhouse(null,h,3,null,true,new Date(),etime);
            sh.setOnline(true);
            shd.insert(sh);
            MapJz m = new MapJz(null,h,112.248+(h.getId()*0.2),30.34+(h.getId()*0.2));
            mapJzDao.insert(m);
            s.setAttribute("imgPaths",null);
            return true;
        }else{
            return false;
        }
    }

    @RequestMapping("/save3")
    @ResponseBody
    public Boolean save3(BuyHouse buyHouse,int[] names,Integer etime){
        Date d = new Date();
        Timestamp t = new Timestamp(d.getTime());
        buyHouse.setBeginTime(t);
        return bhd.insert(buyHouse)>0;
    }

    @RequestMapping("/save4")
    @ResponseBody
    public Boolean save4(BuyHouse buyHouse,int[] names,Integer etime){
        Date d = new Date();
        Timestamp t = new Timestamp(d.getTime());
        buyHouse.setBeginTime(t);
        return bhd.insert(buyHouse)>0;
    }
}




